package cn.offcn.service.Impl;

import cn.offcn.entity.RoleExample;
import cn.offcn.entity.RoleSourcesExample;
import cn.offcn.entity.Sources;
import cn.offcn.entity.SourcesExample;
import cn.offcn.mapper.RoleSourcesMapper;
import cn.offcn.mapper.SourcesMapper;
import cn.offcn.service.SourcesService;
import cn.offcn.utils.OAResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author yuliantao
 * @create 2020 -09 -2020-09-17 21:21
 * @description 功能描述
 */
@Service

public class SourcesServiceImpl implements SourcesService {

    @Autowired
    private RoleSourcesMapper roleSourcesMapper;

    @Autowired
  private   SourcesMapper sourcesMapper ;
/**
 * 功能描述: <br>
 * 〈查找所有元素〉
 * @Param: [pid]
 * @Return: java.util.List<cn.offcn.entity.Sources>
 * @Author: Dong
 * @Date: 2020-09-17 21:23
 */
    @Override
    public List<Sources> getRootSourcesByPid(int pid) {
        SourcesExample sourcesExample = new SourcesExample();
        SourcesExample.Criteria criteria = sourcesExample.createCriteria();
        criteria.andPidEqualTo(pid);
        List<Sources> sourcesList = sourcesMapper.selectByExample(sourcesExample);
        for (Sources source : sourcesList) {
            List<Sources> rootSourcesByPid = getRootSourcesByPid(source.getId());
            source.setChildren(rootSourcesByPid);
        }
        return sourcesList;
    }


    @Override
    public List<Sources> getParentSources(int pid ,List<Sources> sourcesList) {
        SourcesExample sourcesExample = new SourcesExample();
        SourcesExample.Criteria criteria = sourcesExample.createCriteria();
        criteria.andPidEqualTo(pid);
        List<Sources> resultList = sourcesMapper.selectByExample(sourcesExample);
        for (Sources sources : resultList) {
            if(isParentNode(sources.getId())){
                sourcesList.add(sources);
            }
            getParentSources(sources.getId(),sourcesList);
        }
        return sourcesList;
    }
    public boolean isParentNode(int pid){
        SourcesExample sourcesExample=new SourcesExample();
        SourcesExample.Criteria criteria = sourcesExample.createCriteria();
        criteria.andPidEqualTo(pid);
        List<Sources> resultList1= sourcesMapper.selectByExample(sourcesExample);
        if(resultList1!=null && resultList1.size()>0){
            return true;
        }
        return false;
    }

    @Override
    public OAResult saveSources(Sources sources) {
        return null;
    }

    @Override
    public OAResult deleatSourceById(int id) {
        RoleSourcesExample roleSourcesExample = new RoleSourcesExample();
        RoleSourcesExample.Criteria criteria = roleSourcesExample.createCriteria();
        criteria.andResourcesFkEqualTo(id);
        //删除中间表记录
        roleSourcesMapper.deleteByExample(roleSourcesExample);
        int i = sourcesMapper.deleteByPrimaryKey(id);
        if (i==1){
            return OAResult.ok(200,"成功");
        }
        return OAResult.ok(400,"失败");


    }

    @Override
    public Sources getSourcesById(int id) {

        return sourcesMapper.selectByPrimaryKey(id);
    }

    @Override
    public OAResult updeteSourcesById(Sources sources) {

        int i = sourcesMapper.updateByPrimaryKey(sources);
        if (i==1){
            return OAResult.ok(200,"修改成功！");
        }

        return OAResult.ok(400,"修改失败");
    }

    @Override
    public List<Sources> querySourcesByEidAndPid(int eid, int pid) {
        return sourcesMapper.querySourcesByEidAndPid(eid,pid);

    }


    @Override
    public List<Sources> getSourcesByCache(int eid, int pid) {
        List<Sources> sourcesList = getSourcesByEidAndPid(eid, pid);


        return sourcesList;
    }

    public List<Sources> getSourcesByEidAndPid(int eid, int pid) {

        List<Sources> sourcesList = sourcesMapper.querySourcesByEidAndPid(eid, pid);

        for (Sources sources : sourcesList) {
            List<Sources> childrenList = getSourcesByEidAndPid(eid, sources.getId());
            sources.setChildren(childrenList);

        }

        return sourcesList;
    }
}
